package org.jili.novoland.base;

import org.jili.novoland.util.Cfg;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Menu extends Layer implements Actor {

    static public Rect srcBGRect, dstBGRect;

    // private int col, row;

    protected MenuItem[] items;
    protected int selected = -1;

    protected Bitmap img;

    public Menu(Bitmap inImg) {
        super(0, 0, inImg.getWidth(), inImg.getHeight());
        setImage(inImg);
    }

    public void setItems(MenuItem inItems[]) {
        items = inItems;
    }

    public int checkSelected(int x, int y) {
        selected = -1;
        for (int i = 0; i < items.length; i++) {
            if (items[i].checkSelected(x, y)) {
                selected = i;
                break;
            }
        }
        return selected;
    }

    public void setImage(Bitmap inImg) {
        if (inImg == null)
            throw new NullPointerException();

        this.img = inImg;
        srcBGRect = new Rect(0, 0, img.getWidth(), img.getHeight());
        dstBGRect = new Rect(0, 0, Cfg.getViewWidth(), Cfg.getViewHeight());
    }

    public Bitmap getImage() {
        return img;
    }

    @Override
    public void tick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void paint(int viewX, int viewY, Canvas c) {
        if (!isVisible() || items == null || items.length < 1)
            return;
        c.drawBitmap(img, srcBGRect, dstBGRect, null);
        for (int i = 0; i < items.length; i++) {
            MenuItem item = items[i];
            item.paint(viewX, viewY, c);
        }

    }

}
